//
//  MTCreatePartySelectCell.swift
//  MT
//
//  Created by ooops on 2020/5/23.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import Foundation
import Eureka
import MTCategory

public final class MTPartyTypeSelectRow: MTCreatePartyTypeSelectRow, RowType {
    required public init(tag: String?) {
        super.init(tag: tag)
    }
}

open class MTCreatePartyTypeSelectRow: MTCreatePartyTypeSelectCellRow {
    required public init(tag: String?) {
        super.init(tag: tag)
    }
}

extension MTCreatePartyTypeSelectCell: MTPartyTypeViewDelegate {    
    func typeSelectDidRemoveFromSuperView(value: String) {
        if value.count > 0 {
            self.rowValue = value
        }
        self.resignFirstResponder()
    }
}

open class MTCreatePartyTypeSelectCell: Cell<String>, CellType {

    public required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    /// 显示模式
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "名称"
        l.textAlignment = .right
        l.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        return l
    }()
    
    lazy var contentLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = UIColor.text.x9b9b9b
        l.numberOfLines = 0
        l.lineBreakMode = .byCharWrapping
        l.textAlignment = .right
        return l
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        v.isHidden = true
        return v
    }()
    
    lazy var customAccessoryView: UIImageView = {
            let imageV = UIImageView(image: UIImage(named: "icon_right_gary"))
            return imageV
        }()
    
    public var tipString: String? = nil
    public var contentString: String? = nil
    /// 输入框提示文字
    public var placeholder: String? {
        didSet {
            if placeholder != nil {
                let attribute: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : PingFangSCMediumFont(14),
                                                                 NSAttributedString.Key.foregroundColor : UIColor.text.x9b9b9b];
                contentLabel.attributedText = NSAttributedString(string: placeholder!, attributes: attribute)
            }
        }
    }
    
    public var isHiddenAccessory: Bool = true {
        didSet {
            customAccessoryView.isHidden = isHiddenAccessory
        }
    }
    
    public var accessoryImage: String? {
        didSet {
            if accessoryImage != nil {
                customAccessoryView.image = UIImage(named: accessoryImage!)
            } else {
                customAccessoryView.image = nil
            }
        }
    }
    
    private var isHiddenLineView: Bool = true {
        didSet {
            lineView.isHidden = isHiddenLineView
        }
    }
    
    public var isEditMode: Bool = false {
        didSet {
            lineView.isHidden = !isEditMode
            isHiddenAccessory = !isEditMode
        }
    }
    
    private var rowValue: String? {
        didSet {
            self.contentLabel.text = rowValue
            row.value = rowValue
        }
    }
    
    open override func setup() {
        super.setup()
        backgroundColor = UIColor.theme.background
        selectionStyle = .none
        textLabel?.isHidden = true
        detailTextLabel?.isHidden = true
        editingAccessoryType =  .none
        
        contentView.addSubview(tipLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(lineView)
        contentView.addSubview(customAccessoryView)
        
        makeConstraint()
    }
    
    
    
    private func makeConstraint() {
        lineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.width.greaterThanOrEqualTo(30)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.right.equalTo(customAccessoryView.snp.left).offset(-8)
            make.left.equalTo(tipLabel.snp.right).offset(14)
            make.centerY.equalTo(tipLabel)
        }

        customAccessoryView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-8)
            make.centerY.equalTo(tipLabel)
            make.width.height.equalTo(15)
        }
    }

    deinit {
//        datePicker.removeTarget(self, action: nil, for: .allEvents)
    }
    
    lazy var typeView: MTPartyTypeView = {
        let v = MTPartyTypeView()
        v.delegate = self
        v.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
        v.backgroundColor = UIColor(hex: 0x000000, alpha: 0.5)
        return v
    }()
    

    open override func update() {
        super.update()
        tipLabel.text = tipString
        if row.value != nil {
            contentLabel.text = row.displayValueFor?(row.value)
            contentLabel.font = PingFangSCSemiboldFont(14)
            contentLabel.textColor = UIColor.text.x9b9b9b
        }
    }
    
    /// 展示弹窗
    public func present(animated: Bool = true, view: UIView) {
        //UIApplication.shared.keyWindow?.rootViewController?.view.addSubview(self)
        if let p = UIApplication.mt_keyWindow?.rootViewController?.presentedViewController {
            p.view.addSubview(self)
        } else {
            UIApplication.shared.keyWindow?.rootViewController?.view.addSubview(view)
        }
        view.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
        
//        view.frame = CGRect(x: 0, y: ScreenHeight, width: ScreenWidth, height: ScreenHeight)
        
//        UIView.animate(withDuration: animated ? 0.25 : 0, animations: {
//
//            view.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
//
//        }) { (flag) in
//
//        }
    }

    open override func didSelect() {
        super.didSelect()
//        row.deselect()
        
        let v = MTPartyTypeView()
        v.delegate = self
        v.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
        v.backgroundColor = UIColor(hex: 0x000000, alpha: 0.5)
        
        self.typeView = v
        
        self.formViewController()?.tableView.endEditing(true)
        self.present(animated: true, view: typeView)
    }

//    override open var inputView: UIView? {
//        if isEditMode {
//            return pickerView
//        }
//        return nil
//    }

    @objc func datePickerValueChanged(_ sender: UIDatePicker) {
        contentLabel.text = row.displayValueFor?(row.value)
    }
    open override func cellCanBecomeFirstResponder() -> Bool {
        return canBecomeFirstResponder
    }

    override open var canBecomeFirstResponder: Bool {
        return isEditMode
    }
}

open class MTCreatePartyTypeSelectCellRow: Row<MTCreatePartyTypeSelectCell>, NoValueDisplayTextConformance {

    open var noValueDisplayText: String? = nil

    required public init(tag: String?) {
        super.init(tag: tag)
        displayValueFor = { [unowned self] value in
            guard let val = value else { return nil }
            return val
        }
    }
}


